package com.example.fireassistant_server.repository.spec;

import com.example.fireassistant_server.entity.FireHydrant;
import com.example.fireassistant_server.entity.PolyGon;
import com.example.fireassistant_server.entity.PolyLine;

import javax.persistence.criteria.CriteriaBuilder;
import javax.persistence.criteria.CriteriaQuery;
import javax.persistence.criteria.Predicate;
import javax.persistence.criteria.Root;
import java.util.ArrayList;
import java.util.List;

public class PoloGonSpecifation extends AbstractSpecification<PolyGon>{
    private final List<PointSearchCriteria> list;

    public PoloGonSpecifation() {
        this.list = new ArrayList<>();
    }

    public void add(PointSearchCriteria searchCriteria) {
        list.add(searchCriteria);
    }

    @Override
    public Predicate toPredicate(Root<PolyGon> root, CriteriaQuery<?> query, CriteriaBuilder builder) {
        List<Predicate> predicates = new ArrayList<>();
        for (PointSearchCriteria criteria : list) {
            if (criteria.getOperation().equals(SearchOperation.BETWEEN)) {
                predicates.add(builder.between(root.get(criteria.getKey()), criteria.getMinValue(), criteria.getMaxValue()));
            }
        }
        return builder.and(predicates.toArray(new Predicate[0]));
    }
}
